# -*- coding: utf-8 -*-
from . import sync_log


from odoo import models, fields, api
import logging

_logger = logging.getLogger(__name__)


# 修改基础模型继承方式
class BaseSyncModel(models.AbstractModel):
    _name = 'base.sync.model'
    _description = '同步基础模型'

    def _append_sync_log(self, message, log_level='info'):
        """ 增强版日志记录 - 使用中间表关联 """
        try:
            # 创建日志记录
            log = self.env['sync.log'].sudo().create({
                'content': f"[{fields.Datetime.now()}] {message}",
                'log_level': log_level,
                'model_name': self._name,
            })

            # 创建关联记录
            self.env['sync.log.reference'].sudo().create({
                'log_id': log.id,
                'model_name': self._name,
                'record_id': self.id,
            })
        except Exception as e:
            _logger.error(f"日志记录失败: {str(e)}")